<?php
/**
 * 
 * @authors Lin (callbackbing@163.com)
 * @date    2014-01-11 11:47:20
 * @version $Id$
 */

class SettingAction extends CommonAction {

    /*搜索引擎比例设置*/
    public function index(){

        $agent_id = $_SESSION['agent']['id'];
        $yq_arr = M('proxyengine')->where("proxyid=$agent_id")->field('enyinqing,bili')->select();

        $arr = array();
        foreach ($yq_arr as $k => $v) {
            $arr[$v['enyinqing']].=$v['bili'];
        }

        $this->assign('baidu',$arr['baidu']);
        $this->assign('qihoo',$arr['qihoo']);
        $this->assign('soso',$arr['soso']);
        $this->assign('sogou',$arr['sougou']);
        $this->display();
    }

    /*修改搜索引擎比例设置*/
    public function yq_save(){

        $agent_id = $_SESSION['agent']['id'];
        $data['baidu'] = I('post.bai');
        $data['qihoo'] = I('post.qi');
        $data['soso'] = I('post.so');
        $data['sougou'] = I('post.gou');
        
        foreach ($data as $k => $v) {
            $arr['bili'] = $v;
            $result = M('proxyengine')->where("proxyid=$agent_id and enyinqing='".$k."'")->save($arr);
        }

        if($result !== FALSE){
            $ajax['status'] = 'true';
            $ajax['Msg'] = '资料修改成功!';
            echo json_encode($ajax);
        }else{
            $ajax['Msg'] = '资料修改失败!';
            echo json_encode($ajax);   
        }
        
    }

    /*排名比例设置*/
    public function setting_bili(){
        $agent_id = $_SESSION['agent']['id'];
        $page_arr = M('proxy')->where("id=$agent_id")->field('pageonerate,pagetworate,pagethreerate')->find();

        $this->assign('one',$page_arr['pageonerate']);
        $this->assign('two',$page_arr['pagetworate']);
        $this->assign('three',$page_arr['pagethreerate']);
        $this->display();
    }

    /*修改排名比例设置*/
    public function setting_bili_save(){

        $agent_id = $_SESSION['agent']['id'];
        $data['pageonerate'] = I('post.one');
        $data['pagetworate'] = I('post.two');
        $data['pagethreerate'] = I('post.three');
        $result = M('proxy')->where("id=$agent_id")->save($data);

        if($result !== FALSE){
            $ajax['status'] = 'true';
            $ajax['Msg'] = '资料修改成功!';
            echo json_encode($ajax);
        }else{
            $ajax['Msg'] = '资料修改失败!';
            echo json_encode($ajax);            
        }
    }

    /*客户类型设置*/
    public function setting_client(){

        $agent_id = $_SESSION['agent']['id'];
        $arr = M('acounttype')
        ->where("proxyid in(0,$agent_id)")
        ->field("id,name,money,proxyid")
        ->order('id desc')->select();
        $this->assign('arr',$arr);
        $this->display();
    }

    /*修改客户类型*/
    public function client_save(){

        $id = I('get.id');
        $agent_id = $_SESSION['agent']['id'];
        $this->clientid = $id;

        if(IS_POST){
            $clientid = I('post.clientid');
            $data['name'] = I('post.client_type');
            $data['money'] = I('post.charge');

            $rs = M('acounttype')->where("name='".$data['name']."' and proxyid in (0,$agent_id)")->find();

            if(!$rs){
                $result = M('acounttype')->where("id=$clientid")->save($data);

                if($result!==FALSE){
                    $ajax['status'] = 'true';
                    $ajax['Msg'] = '资料修改成功!';
                    echo json_encode($ajax);
                }else{
                    $ajax['Msg'] = '资料修改失败!';
                    echo json_encode($ajax);            
                }                
            }else{
                    $ajax['Msg'] = '已经存在此类型客户!';
                    echo json_encode($ajax);                  
            }

            die();
        }

        $arr = M('acounttype')->where("id=$id and proxyid=$agent_id")->find();

        if(!$arr){
            $this->error('您没有权限访问!');
        }

        $this->assign('arr',$arr);
        $this->display();
    }

    /*处理客户类型修改方法*/
    public function client_del(){

        if(IS_POST){
            $agent_id = $_SESSION['agent']['id'];
            $id = I('post.userId');
            $result = M('acounttype')->where("id=$id and proxyid=$agent_id")->delete();

            if($result){
                $ajax['status'] = 'true';
                $ajax['Msg'] = '删除客户类型成功!';
                echo json_encode($ajax);
            }else{
                $ajax['Msg'] = '删除客户类型失败!';
                echo json_encode($ajax);
            }
        }else{
            $this->error('您没有权限访问!');  
        }          
    }

    /*添加客户类型*/
    public function client_add(){

        $agent_id = $_SESSION['agent']['id'];

        if(IS_POST){
            $arr['keywords'] = I('post.keywords');
            $arr['moneys'] = I('post.moneys');

            $i = 0;
            while ($i<count($arr['keywords'])) {

                $data['proxyid'] = array("in","0,$agent_id");
                $data['name'] = $arr['keywords'][$i];

                $rs = M('acounttype')->where($data)->find();
                unset($data['proxyid']);
                if(!$rs){
                    $data['proxyid'] = $agent_id;
                    $data['money'] = $arr['moneys'][$i];
                    $result = M('acounttype')->add($data);
                }

                $i++;
            }

            if($result){
                $ajax['status'] = 'true';
                $ajax['Msg'] = '资料提交成功!';
                $ajax['url'] = '/Setting/setting_client';
                echo json_encode($ajax);
            }else{
                $ajax['Msg'] = '已经存在此类型客户!';
                echo json_encode($ajax);   
            }

            die();
        }

        $this->display();
    }

    public function set_yq(){
        $agent_Model = D('proxy');
        $arr['username'] = $_SESSION['agent']['username'];
        $data['pageonerate'] = I('post.pageonerate');
        $data['pagetworate'] = I('post.pagetworate');
        $data['pagethreerate'] = I('post.pagethreerate');

        if($agent_Model->create()){
            $result = $agent_Model->where($arr)->save($data);
        }else{
            $this->error($agent_Model->getError());
        }
        unset($data);
        unset($result);

        $yinqing['baidu'] = I('post.bili_baidu');
        $yinqing['qihu'] = I('post.bili_qihu');
        $yinqing['soso'] = I('post.bili_soso');
        $yinqing['sougou'] = I('post.bili_sogou');
        $agent_id = $_SESSION['agent']['id'];

        foreach ($yinqing as $k => $v) {
            switch ($k) {
                case 'baidu':
                    $data['enginename'] = '百度';
                    $data['yinqing'] = '百度';
                    $data['enyinqing'] = 'baidu';
                    $data['proxyid'] = $agent_id;
                    $data['bili'] = $v;
                    break;
                case 'qihu':
                    $data['enginename'] = '360';
                    $data['yinqing'] = '360';
                    $data['enyinqing'] = 'qihoo';
                    $data['proxyid'] = $agent_id;
                    $data['bili'] = $v;
                    break;
                case 'sougou':
                    $data['enginename'] = '搜狗';
                    $data['yinqing'] = '搜狗';
                    $data['enyinqing'] = 'sougou';
                    $data['proxyid'] = $agent_id;
                    $data['bili'] = $v;
                    break;
                case 'soso':
                    $data['enginename'] = '搜搜';
                    $data['yinqing'] = '搜搜';
                    $data['enyinqing'] = 'soso';
                    $data['proxyid'] = $agent_id;
                    $data['bili'] = $v;
                    break;                                         
            }
            $yq_model = M('proxyengine');
            $result = $yq_model->add($data);
        }
        if($result){
            $this->success('资料提交成功',U('Index/index'));
        }else{
            $this->error('资料提交失败');
        }
    }
}